package com.kuns.subject.domian.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.kuns.subject.common.enums.IsDeletedFlagEnum;
import com.kuns.subject.domian.convert.SubjectCategoryConverter;
import com.kuns.subject.domian.entity.SubjectCategoryBO;
import com.kuns.subject.domian.service.SubjectCategoryDomainService;
import com.kuns.subject.infra.basic.entity.SubjectCategory;
import com.kuns.subject.infra.basic.service.SubjectCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class SubjectCategoryDomainServiceImpl implements SubjectCategoryDomainService{

    @Resource
    private SubjectCategoryService subjectCategoryService;

    @Override
    public void add(SubjectCategoryBO subjectCategoryBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectCategoryDomainServiceImpl.add.bo:{}", JSONObject.toJSONString(subjectCategoryBO));
        }
        SubjectCategory subjectCategory = SubjectCategoryConverter.INSTANCE
                .convertBoToCategory(subjectCategoryBO);
        subjectCategory.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.code);
        subjectCategoryService.insert(subjectCategory);
    }

    /**
     * 查询大类 categoryType=1，就是查最大类，后端、前端就是最大类
     * @param subjectCategoryBO
     * @return
     */
    @Override
    public List<SubjectCategoryBO> queryCategory(SubjectCategoryBO subjectCategoryBO) {
        // 将BO转换成entity调用infra层的服务
        SubjectCategory subjectCategory = SubjectCategoryConverter.INSTANCE.convertBoToCategory(subjectCategoryBO);
        List<SubjectCategory> subjectCategoryList = subjectCategoryService.queryCategory(subjectCategory);
        // 将entity转换成bo返回
        List<SubjectCategoryBO> subjectCategoryBOList = SubjectCategoryConverter.INSTANCE.convertCategoryListToBoList(subjectCategoryList);
        if (log.isInfoEnabled()) {
            log.info("SubjectCategoryDomainServiceImpl.queryPrimaryCategory.boList:{}", JSONObject.toJSONString(subjectCategoryBOList));
        }
        return subjectCategoryBOList;
    }

    @Override
    public boolean update(SubjectCategoryBO subjectCategoryBO) {
        // 将bo转成entity调用infra层的服务
        SubjectCategory subjectCategory = SubjectCategoryConverter.INSTANCE.convertBoToCategory(subjectCategoryBO);
        final int count = subjectCategoryService.update(subjectCategory);
        return count > 0;
    }

    @Override
    public Boolean delete(SubjectCategoryBO subjectCategoryBO) {
        // 将bo转换成entity调用infra层服务
        SubjectCategory subjectCategory = SubjectCategoryConverter.INSTANCE.convertBoToCategory(subjectCategoryBO);
        subjectCategory.setIsDeleted(IsDeletedFlagEnum.DELETED.code); // 逻辑删除
        int count = subjectCategoryService.update(subjectCategory);
        return count > 0;
    }


}
